# Runtime tests of parts and inputs of a ProtocolApplication. 
# 
###############################################################################

#' Sanity test for action
#' 
#' Unimplemented, just returns TRUE
#' @param action A closure that will be tested.
#' @author Bastian R. Angermann
isSaneActionImplementation <- function(action)
{
  
    # TODO: sanity test codetools should return no issues and nothing
    # in the parent environment must be changed. guidelines must be 
# respected 
    # check arguments checkUssage findGlobals
    TRUE
}

# see documentation below
setGeneric(name="test",
        def=function(protocolApplication) standardGeneric("test") )
#' Test pipeline execution.
#' 
#' Test the execution of a pipeline. Mock output files are created. Basic tests on input files will be done
#' in later versions. Detailed tests if input files are valid will not be done as this will be to expensive and
#' turn around times would be similar to a real pipeline execution.
#' @param protocolApplication The protocol application to be tested
#' @author Bastian R. Angermann 
setMethod("test", signature(protocolApplication="ProtocolApplication"), function (protocolApplication){
    tryCatch({ # User interrupt
        if (getwd() != protocolApplication@workingDirectory)
            setWorkingDirectory( protocolApplication@workingDirectory)
        actionName <- attr(protocolApplication@.action, which="name")
        if (actionName == "protocolList") # Ignore input data execute child protocols
        {  # TODO add summary data
            lapply(X=protocolApplication@.childProtocolApplications, FUN=function(x) {
                   oldWorkingDirectory <- getwd()
                   test(x)
                   setWorkingDirectory(oldWorkingDirectory)
                   })
        }
        else { # execute the Action:
                currentFile <- 0
                baseName <- getBaseName(protocolApplication)
                if (is.null(protocolApplication@.inputData)) # Action has no input Data
                {
                        result <- "Test: action with no input data"
                        fileName <- baseName
                        if (!is.null(protocolApplication@outputData))
                                writeData(result$main, fileName)
                        else
                                fileName <<- result$main
                } else
                {   
                    InputDataSetNo <- length(protocolApplication@.inputData)
                    outputFiles <- vector() 
                    while (currentFile < InputDataSetNo ) # Action has input data
                    {
                                currentFile <- currentFile + 1
                                currentFileList <- protocolApplication@.inputData[[currentFile]]
                                # TODO do some testing to file format declarations here. write correct format declarations 
                                # in mock up output
                                result <- paste("Test: with input data:",unlist(currentFileList))
                                fileNumber <- pad(currentFile,ceiling(log( InputDataSetNo,base=10)))
                                fileName <- paste(baseName, fileNumber, sep="_")
                                outputFiles <- c(outputFiles,fileName)
                                if (!is.null(protocolApplication@outputData))
                                        writeData(result$main,fileName)
                                else
                                        fileName <<- result$main
                    }
                    listFileName <- getListFileName(protocolApplication)
                        if (!is.null(protocolApplication@outputData))
                                writeCsvFileList(file=listFileName,outputFiles)
                        else
                                listFileName <<- outputFiles
            }
        }
    }, # end of try block
    interrupt=function(x){submitToLog("Pipeline test interrupted","Error") } ) 
    invisible(NULL)
})
